
/******************************************************************
Copyright © PsuCtl 2017~2023. All rights reserved.
* @projectName   PsuCtl
* @author        Raiiwhen
* @email         masterraii@icloud.com
* @origin        ESP i2c-tools expamle
*******************************************************************/

#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_fat.h"
#include "cmd_system.h"
#include "cmd_i2ctools.h"
#include "driver/i2c_master.h"
#include "gui.h"
#include "PMBus.h"
#include "ads128d818.h"
#include "network.h"
#include "beacon.h"

static const char *TAG = "mainapp";

static gpio_num_t i2c_gpio_sda = 4;
static gpio_num_t i2c_gpio_scl = 5;

extern void register_i2cconfig(void);
extern void register_i2cdetect(void);
extern void register_i2cget(void);
extern void register_i2cset(void);
extern void register_i2cdump(void);
void MainLoop(void);

void app_main(void)
{
    esp_console_repl_t *repl = NULL;
    esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();

    repl_config.prompt = "PsuCtl>";

    esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
    esp_console_new_repl_uart(&uart_config, &repl_config, &repl);

    i2c_master_bus_config_t i2c_bus_config = {
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .i2c_port = I2C_NUM_0,
        .scl_io_num = i2c_gpio_scl,
        .sda_io_num = i2c_gpio_sda,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true,
    };

    i2c_new_master_bus(&i2c_bus_config, &tool_bus_handle);

    register_i2cconfig();
    register_i2cdetect();
    register_i2cget();
    register_i2cset();
    register_i2cdump();

    panel_init();
    PMBusConfig(0x5f, 0x57, tool_bus_handle, 400 * 1000);
    AdsConfig(0x1d, tool_bus_handle, 400 * 1000);
    NvfsConfig();
    WiFiConfig("raii2022", "20221801");
    xTaskCreate(&BeaconAliveReport, "beacon_alive", 4096, NULL, 5, NULL);

    printf("\n ==============================================================\n");
    printf(" |             Steps to Use i2c-tools                         |\n");
    printf(" |                                                            |\n");
    printf(" |  1. Try 'help', check all supported commands               |\n");
    printf(" |  2. Try 'i2cconfig' to configure your I2C bus              |\n");
    printf(" |  3. Try 'i2cdetect' to scan devices on the bus             |\n");
    printf(" |  4. Try 'i2cget' to get the content of specific register   |\n");
    printf(" |  5. Try 'i2cset' to set the value of specific register     |\n");
    printf(" |  6. Try 'i2cdump' to dump all the register (Experiment)    |\n");
    printf(" |                                                            |\n");
    printf(" ==============================================================\n\n");

    esp_console_start_repl(repl);
    MainLoop();
}

void MainLoop(void)
{
    float currOut[8];
    while (1)
    {
        // PMBusSyncStatus();
        const smbus_info_t* dev = GetPMBusInfoInstance();
        AdsReadVoltage(currOut);
        for (int ch = 0; ch < 8; ch++) {
            // IN180-A2 IDVBR 50x
            // Res 10mR
            // transimpedance = 0.5R
            currOut[ch] /= 0.5f; // Unit: Amp
        }
        GuiUpdateInfo(currOut);
        UpdateNetWorkState();
        BeaconAliveInfoConfig("BEACON_PROTOTYPE", GetNetworkIP(), GetRssi());
        vTaskDelay(50);
    }
}
